package com.gjy.concurrency.future;

import org.junit.jupiter.api.Test;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

/**
 * @author 宫静雨
 * @version 1.0
 * @since 2022-11-29 08:36:30
 */
public class CompletableFutureTest {

    public static void main(String[] args) throws Exception {
        test4();
    }

    @Test
    public void test6() {

    }

    @Test
    public void test5() throws ExecutionException, InterruptedException {
        CompletableFuture<Double> weightFuture = CompletableFuture.supplyAsync(() -> {
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            return 65.0;
        });
        CompletableFuture<Double> heightFuture = CompletableFuture.supplyAsync(() -> {
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            return 177.8;
        });

        CompletableFuture<Double> bmi = weightFuture.thenCombine(heightFuture, (w, h) -> {
            double him = h / 100;
            return w / (him * him);
        });
        System.out.println("bmi.get() = " + bmi.get());
    }

    public static void test4() throws ExecutionException, InterruptedException {
        CompletableFuture.supplyAsync(() -> "Hello")
                .thenApply(String::toCharArray)
                .thenRun(() -> System.out.println("Hello World")).get();
    }

    public static void test3() throws ExecutionException, InterruptedException {
        CompletableFuture<String> supplyAsync = CompletableFuture.supplyAsync(() -> {
            System.out.println("supplyAsync");
            return "Hello";
        });
        System.out.println("supplyAsync.get() = " + supplyAsync.get());
        System.out.println("supplyAsync.complete(\"world\") = " + supplyAsync.complete("world"));
        System.out.println("supplyAsync.get() = " + supplyAsync.get());
    }

    public static void test2() throws ExecutionException, InterruptedException {
        CompletableFuture<Void> runAsync = CompletableFuture.runAsync(() -> {
            try {
                System.out.println("CompletableFuture runAsync 1");
                TimeUnit.SECONDS.sleep(2);
                System.out.println("CompletableFuture runAsync 2");
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        });
        System.out.println(runAsync.get());
    }

    public static void test1() {
        CompletableFuture<String> future = new CompletableFuture<>();
        new Thread(new T1(future)).start();
        new Thread(new T2(future, "Hello CompletableFuture")).start();
    }

    private static final class T1 implements Runnable {

        private final CompletableFuture<String> future;

        public T1(CompletableFuture<String> future) {
            this.future = future;
        }

        @Override
        public void run() {
            try {
                System.out.println("future.get() = " + future.get());
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
    }

    private static final class T2 implements Runnable {

        private final CompletableFuture<String> future;
        private final String value;

        public T2(CompletableFuture<String> future, String value) {
            this.future = future;
            this.value = value;
        }

        @Override
        public void run() {
            try {
                TimeUnit.SECONDS.sleep(3);
                System.out.println("future.complete(value) = " + future.complete(value));
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
    }

}
